var changeShiftsJs = function (targetObj) {
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	this.dateFormat = "yyyy-MM-dd HH:mm:ss";
	return this;
};

changeShiftsJs.prototype = {
		init : function () {
			var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find('.search-panel'));
			var functionArray = [
			                     {fName : 'search_btn' , fs : this.findReport},
			                     {fName : 'reset_btn' , fs : this.resetInfo}
			                     ];
			this.models = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.models, this.targetObj, this);
			this.initHtml();
			//默认查最近一个月的数据
			var date = new Date();
			date.setHours(8, 0, 0, 0);
			this.sysUtils.mvvmModel.set("endTime",kendo.toString(date,this.dateFormat));
			var bgTime = new Date();
			bgTime.setHours(8, 0, 0, 0);
			bgTime.setDate(bgTime.getDate() - 1);
			this.sysUtils.mvvmModel.set("beginTime",kendo.toString(bgTime,this.dateFormat));
			//计算显示区域高度
			this.targetObj.find("#changeShiftsReport").height(this.targetObj.height() - this.targetObj.find(".search-panel").outerHeight());
		},
		/**
		 * 初始化界面元素
		 */
		initHtml:function(){
			this.userInfo = null;
			var that = this;
			var autoFlag = false;
			var selectFun = function(e){
				that.userInfo = this.dataItem(e.item);
				autoFlag = true;
			};
			var changeFun = function(e){
				if(!autoFlag){
					that.userInfo = null;
					that.targetObj.find("#cashierId").val("");
				}
				autoFlag = false;
			};
			this.sysUtils.autoCompleteUser("cashierId", "userName",selectFun,changeFun);
			/**
			 * 获取所有工作站节点树
			 */
			JsonpAjax(baseApiPath + 'layout/wsn/get', '', 'json', function(res) {
				var potionTree = that.targetObj.find("#parkFee-tree").lonixFloat({
					source: res,
					setting: {
						view: {
							showLine: true,
							selectedMulti: false,
							dblClickExpand: false,
							showIcon: false
						},
						data : {
							simpleData: {
								enable: true,
								idKey: "orgCode",
								pIdKey: "parentCode",
								rootPId:""
							}
						}
					},
					searchField: 'name',
					target: that.targetObj.find("#orgName"),
					primary: function (nodes) {
						if(nodes.length <= 0 ) return;
						that.targetObj.find('#parkFee-tree').fadeUp();
						that.parkOrgCode = nodes[0]["orgCode"];
						//显示层级
						that.targetObj.find("#orgName").val(nodes[0]["name"]+"("+that.parkOrgCode+")");
					}
				});
			}, false);
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
			that.userInfo = null;
			that.targetObj.find("#cashierId").val("");
			that.parkOrgCode = null;
			that.targetObj.find("#orgName").val("");
			//默认查最近一个月的数据
			var date = new Date();
			that.sysUtils.mvvmModel.set("endTime",kendo.toString(date,that.dateFormat));
			date.setMonth(date.getMonth() - 1);
			that.sysUtils.mvvmModel.set("beginTime",kendo.toString(date,that.dateFormat));
		},
		/**
		 * 获取查询参数
		 * @param params
		 */
		getQueryParams:function(params){
			if(isEmpty(params["beginTime"]) || isEmpty(params["endTime"])){
				notification({title : '提示', message : '开始时间和结束时间为必填，请选择！！'}, 'info');
				return;
			}
			params["beginTime"] = kendo.toString(params["beginTime"],this.dateFormat);
			params["endTime"] = kendo.toString(params["endTime"],this.dateFormat);
			//检查查询时间段是否超过半年
			var beginTime = kendo.parseDate(params["beginTime"], this.dateFormat);
			var endTime =  kendo.parseDate(params["endTime"], this.dateFormat);
			beginTime.setMonth(beginTime.getMonth() + 6);
			if(endTime > beginTime){
				notification({title : '提示', message : '开始时间和结束时间相差不能超过半年！！'}, 'info');
				return;
			}
			if(null != this.userInfo && !isEmpty(this.targetObj.find("#cashierId").val())){
				params["cashierId"] =  this.userInfo.userAccount;
			}
			if(null != this.parkOrgCode){
				params["parkOrgCode"] =  this.parkOrgCode;
			}
			return params;
		},
		/**
		 * 查询报表数据
		 * @param e
		 */
		findReport:function(e){
			var that = this;
			if(null != e){
				that = $(e.target).data("that");
			}
			that.targetObj.find("#changeShiftsTable").find("tr:not(:first)").empty();
			that.targetObj.find("#timeReport").html("");
			that.targetObj.find("#totalMoney").html("");
			var params = that.sysUtils.getBindDocJson(that.models.bindFieldArray);
			params = that.getQueryParams(params);
			JsonpAjax(baseApiPath + 'report/changeShifts', JSON.stringify(params), 'json', function (maps) {
				var table = that.targetObj.find("#changeShiftsTable");
				var totaldue = 0;
				var totalPer = 0;
				var wsnName = "";
				//存储起来，核对的时候需要用到
				that.currTableList = new Array();
				for(var names in maps){
					var tr = $("<tr></tr>");
					var list = maps[names];
					var totalDueMoney = 0;
					var totalPerMoney = 0;
					//取工作站第一个上班时间和最后一个下班时间
					if(list.length >= 1){
						var shiftsJson = {};
						shiftsJson["workstationName"] = list[0]["workstationName"];
						shiftsJson["workstationCode"] = list[0]["workstationCode"];
						shiftsJson["userName"] = list[0]["userName"];
						shiftsJson["loginTime"] = list[0]["loginTime"];
						shiftsJson["eixtTime"] = list[list.length - 1]["eixtTime"];
						that.currTableList.push(shiftsJson);
					} 
					for(var i = 0;i<list.length;i++){
						if(i == 0){
							var td = $("<td>"+list[i]["workstationName"] + "(" + list[i]["workstationCode"] + ")" +"</td>");
							td.attr("rowspan",list.length + 1);
							tr.append(td);
						}else{
							tr =  $("<tr></tr>");
						}
						tr.append($("<td>"+list[i]["userName"] + "("+list[i]["userAccount"]+")</td>"));
						tr.append($("<td>"+list[i]["loginTime"]+"</td>"));
						tr.append($("<td>"+list[i]["eixtTime"] +"</td>"));
						tr.append($("<td>"+(list[i]["dueMoney"]+list[i]["perMoney"]) +"</td>"));
						tr.append($("<td>"+list[i]["dueMoney"] +"</td>"));
						tr.append($("<td>"+list[i]["perMoney"] +"</td>"));
						totalDueMoney += list[i]["dueMoney"];
						totalPerMoney += list[i]["perMoney"];
						table.append(tr);  //添加工作站行
					}
					var totalTr = $("<tr></tr>");
					totalTr.append($("<td colspan='3' style='text-align:right;'>小计：</td>"));	
					totalTr.append($("<td>"+(totalDueMoney+totalPerMoney)+"</td>"));	
					totalTr.append($("<td>"+(totalDueMoney)+"</td>"));
					totalTr.append($("<td>"+(totalPerMoney)+"</td>"));
					table.append(totalTr);  //添加合计行
					totaldue += totalDueMoney;
					totalPer += totalPerMoney;
				}
				//添加总合计行
				var totalLastTr = $("<tr></tr>");
				totalLastTr.append($("<td colspan='4' style='text-align:right;'>合计：</td>"));	
				totalLastTr.append($("<td>"+(totaldue+totalPer).toFixed(2)+"</td>"));	
				totalLastTr.append($("<td>"+(totaldue).toFixed(2)+"</td>"));
				totalLastTr.append($("<td>"+(totalPer).toFixed(2)+"</td>"));
				table.append(totalLastTr);  //添加合计行
				//改变title信息
				that.targetObj.find("#timeReport").html(params["beginTime"] +" - " +params["endTime"]);
				that.targetObj.find("#totalMoney").html(totaldue.toFixed(2) + "元");
			}, true);
		},
		/**
		 * 打印
		 * @param e
		 */
		print:function(e){
			this.targetObj.find("#changeShiftsReport").find("div:first").printArea({
				mode:"popup",
				popTitle:"",
				popClose:true,
				popHt:600,
				popWd:1000
			});
		},
		/**
	     * 导出Excel文件
	     */
	    exportExcel:function(){
	    	 var params = this.sysUtils.getBindDocJson(this.models.bindFieldArray);
	    	 params = this.getQueryParams(params);
	         window.open(baseApiPath+'report/exportChangeShiftsExcel?jsonData='+encodeURI(JSON.stringify(params)));
	    },
	    /**
	     * 核对与车场收费统计的数据
	     */
		openInspectionFees:function(){
			this.sysUtils.inspectionFees = this.sysUtils.openWindow(this.sysUtils.inspectionFees, {
                title : "核对车场收费报表",
                height : "570px",
                width : "900px",
                resizable : false,
                content : "views/parkLotCenter/chargeStatistical/ChangeShiftsInspectionFees.html"
            });
		}
};